package com.hnqcgc.controller;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/*
    home 每一次请求的默认方法
 * service：每次请求servlet时都会调用service
 * service方法会调用doGet、doPost方法（HttpServlet重写了service方法调用了doGet、doPost）
 * 重写了service方法就可以不重写doGet、doPost，直接在service中进行了处理
 * 继承BaseServlet的子类Servlet会自动调用service方法
 * */
@WebServlet("/BaseServlet")
public abstract class BaseServlet extends HttpServlet {

    //每一个servlet的默认页
    public  abstract String home(HttpServletRequest request, HttpServletResponse response);

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置编码
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");

        //获取请求参数,调用具体子类的方法
        String methodName=request.getParameter("m");
        System.out.println(methodName);
        if(methodName==null||"".equals(methodName))
        {
            //默认方法为home
            methodName="home";
        }
        //获取当前对象  是子类的Servlet
        Class c=this.getClass();
        System.out.println(c);
        Method method = null ;
        //通过反射获取传入的方法名，获得方法
        //调用指定的子类的方法， 来处理子类的业务逻辑
        try {
            method = c.getMethod(methodName,
                    HttpServletRequest.class,HttpServletResponse.class);
        } catch (NoSuchMethodException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SecurityException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        try {
            //获取之前调用方法的返回值
            String result =(String)method.invoke(this, request,response);

            //处理返回值， 并进行页面跳转
            //规则  f:/a.jsp  转发
            // r:a.jsp  重定向
            if(result!=null){
                int index = result.indexOf(":");
                String operate = result.substring(0,index);
                String path = result.substring(index+1);
                if(result.contains(":")){
                    if(operate.equalsIgnoreCase("r")){
                        System.out.println();
                        response.sendRedirect(request.getContextPath()+"/"+path);
                    }else if(operate.equalsIgnoreCase("f")){
                        request.getRequestDispatcher(path+".jsp").forward(request, response);
                    }
                }
            }
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}
